<?php
namespace services;

use think\helper\Str;
/**
* 
*/
class Payment
{
	private $payment_cls = '';
	protected static $instance = null;

	static public function init($type, $configs = [])
	{
		if (is_null(self::$instance)) {
            self::$instance = new self();
        }
		if ($type) {
			$payment_id = Str::studly($type);//下划线转驼峰
            $_configs = config('pay.'.$payment_id) ? : [];
	    	if (false) {
	    		$configs = array_merge($_configs, $sys_configs, $configs);
	    	}else{
	    		$configs = array_merge($_configs, $configs);
	    	}
			$className = "\\services\\payment\\$payment_id";
			self::$instance->payment_cls = new $className($configs);
		}
		return self::$instance;
	}	
	private function getPaymentInfo()
    {
    	return self::$instance->payment_cls;
    }

	public function doPay($attributes)
	{
        return $this->getPaymentInfo()->pay($attributes);
        
	}
    /**
     * 退款
     * @return [type] [description]
     */
    private function refund($out_trade_no, $total_fee, $refund_fee, $payment_id)
    {
        if ($data = $this->getPaymentInfo()->refund($out_trade_no, $total_fee, $refund_fee)) {
            return $data;
        }
    }
    /**
     * 退除押金
     * @return [type] [description]
     */
    public function doRefundDeposit($order_id, $order_type = 'order')
    {
        if ($order_info = $this->getOrderInfo($order_id, $order_type)) {
            return $this->refund($order_id, $order_info->total_fee, $order_info->deposit, $order_info->payment_id);
        }   
    }
    /**
     * 退除订单款
     * @return [type] [description]
     */
    public function doRefundAll($order_id, $order_type = 'order')
    {
        if ($order_info = $this->getOrderInfo($order_id, $order_type)) {
            return $this->refund($order_id, $order_info->total_fee, $order_info->total_fee, $order_info->payment_id);
        } 
    }
    /**
     * 订单查询
     * @return [type] [description]
     */
    public function doQ($order_id)
    {
    	return $this->getPaymentInfo()->queryOrder($order_id);
        halt($data);
    }
    public function doNotify()
    {
        return $this->getPaymentInfo()->notify();
    }
}